import React, {Component} from 'react';
import NavigationBar from '../../Common/NavigationBar/NavigationBar.jsx'
//import Using ES6 syntax
import WeUI from 'react-weui';
//import styles
import 'weui';
import 'react-weui/build/dist/react-weui.css';
import {CSS} from '../styles/Invite.css';
const {
    Toptips,
    Toast
} = WeUI;
export default class InviteJoin  extends Component {
    constructor(props) {
        super(props);
        this.LeftClick = this.LeftClick.bind(this);
        this.state = {
            showToptips: false,
            showLoading: true,
            showToast: false,
            loadingTimer: null,
            toastTimer:null,
            topTipsMsg: "服务器错误！",
            data:{},
        };
    }

    showToast() {
        this.setState({showToast: true});
        this.state.toastTimer = setTimeout(()=> {
            this.setState({showToast: false});
        }, 2000);
    }

    showWarn(error) {
        this.setState({showToptips: true,topTipsMsg:error, showLoading: false});
        this.state.warnTimer = setTimeout(() => {
            this.setState({showToptips: false});
        }, 2000);
    }

    componentWillMount() { //组件即将被插入
        let href = window.location.href;
        let query = href.split("&");
        let id = query[2].split("=");
        let inviteId = id[1];
        let self = this;
        fetch(getHost() + "/team/group/invitation/"+inviteId+"",{
            method: 'GET',
            credentials: 'include'
        }).then(response => response.json())
            .then(d => {
                if (d.success) {
                    this.setState({data:d.data,showLoading:false})
                } else {
                    self.showWarn(d.error.message);
                }
            });
    }

    LeftClick() {

    }

    inviteCreate(){
        let href = window.location.href;
        let query = href.split("&");
        let id = query[2].split("=");
        let inviteId = id[1];
        location.hash = "/InviteStaffSubmit?inviteId="+inviteId+""
    }

    render() {
        return (
            <div>
                <NavigationBar Title="员工邀请" LeftBar="true" LeftTitle="返回"
                               LeftIconSrc="/iot-meeting/assets/images/common/nav_back_icon.png" LeftClick={this.LeftClick}/>
                <img className="invite_icon" src="/iot-meeting/assets/images/InviteJoin/Invite.png"/>
                <div className="invite_company_title">{this.state.data.teamName}</div>
                <div className="invite_name_title">{this.state.data.createUserName}邀请您加入{this.state.data.teamGroupIdName}</div>
                <div className="accept_invite_btn" onClick={this.inviteCreate.bind(this)}>接受邀请</div>
                <Toast icon="loading" show={this.state.showLoading}>加载中...</Toast>
                <Toptips type="warn" show={this.state.showToptips}>
                    {this.state.topTipsMsg}
                </Toptips>
            </div>
        )
    }
}